package cn.edu.jnu.basicms.mapper;

import cn.edu.jnu.basicms.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    // 新增用户
    @Insert("INSERT INTO user (username, password, name, phone, email, address, avatar) " +
            "VALUES (#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{avatar})")
    void insert(User user);

    // 修改用户
    @Update("UPDATE user SET username=#{username}, password=#{password}, name=#{name}, " +
            "phone=#{phone}, email=#{email}, address=#{address}, avatar=#{avatar} WHERE id=#{id}")
    void updateUser(User user);

    // 删除单个用户
    @Delete("DELETE FROM user WHERE id=#{id}")
    void deleteUser(Integer id);

    // 查询所有用户（带排序）
    @Select("SELECT * FROM user ORDER BY id DESC")
    List<User> selectAll();

    // 按ID查询单个用户
    @Select("SELECT * FROM user WHERE id=#{id}")
    User selectById(Integer id);

    // 按姓名模糊查询
    @Select("SELECT * FROM user WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<User> selectByName(String name);

    // 多条件查询（用户名和姓名）
    @Select("SELECT * FROM user WHERE username=#{username} AND name=#{name}")
    List<User> selectByMore(String username, String name);

    // 分页模糊查询（带排序）
    @Select("SELECT * FROM user " +
            "WHERE username LIKE CONCAT('%', #{username}, '%') AND name LIKE CONCAT('%', #{name}, '%') " +
            "ORDER BY id DESC LIMIT #{skipNum}, #{pageSize}")
    List<User> selectByPage(Integer skipNum, Integer pageSize, String username, String name);

    // 统计分页总记录数
    @Select("SELECT COUNT(id) FROM user " +
            "WHERE username LIKE CONCAT('%', #{username}, '%') AND name LIKE CONCAT('%', #{name}, '%')")
    int selectCount(String username, String name);
}